package cn.nit.blog.service.impl;

import cn.nit.blog.dto.Email;
import cn.nit.blog.entity.User;
import cn.nit.blog.exception.BlogException;
import cn.nit.blog.mapper.UserMapper;
import cn.nit.blog.service.IUserService;
import cn.nit.blog.utils.EmailUtils;
import cn.nit.blog.utils.UUIDUtils;
import cn.nit.blog.vo.UserVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wjx
 * @since 2023-03-21
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    public UserMapper userMapper;

    @Autowired
    public EmailUtils emailUtils;

    @Override
    public void register(UserVo userVo) {
        User userByEmail = getUserByEmail(userVo.getEmail());
        System.out.println("userByEmail = " + userByEmail);  //结果为 null
        if (userByEmail != null) {
            throw new BlogException(1001,"邮箱已被注册");
        }

        System.out.println("userVo.username = " + userVo.getUsername());
        userByEmail = getUserByUsername(userVo.getUsername());
        System.out.println("这是确保用户名的前头");
//        确保用户名的唯一性
        if (userByEmail != null) {
            throw new BlogException(1001,"用户名已被注册");
        }

        System.out.println("后头");
//        else{
////            向数据库插入一条用户信息
//            User user = new User(userVo.getUsername(),userVo.getPwd(),userVo.getEmail());
//
//        }

//        密码加密，将用户其他属性添加，放入数据库
        String gensalt = BCrypt.gensalt();
        String hashpw = BCrypt.hashpw(userVo.getPwd(), gensalt);
        User user = new User();
        user.setUsername(userVo.getUsername());
        user.setPassword(hashpw);
        user.setEmail(userVo.getEmail());
        user.setStatus(0);
        user.setActivationCode(UUIDUtils.getId());
        user.setCreateTime(new Date());

        int insert = baseMapper.insert(user);

//        发邮件激活
        String url = "http://127.0.0.1:8080/user/activation?code="+user.getActivationCode();
        Email email = new Email();
        email.setTitle("账号激活");
        email.setContent("注册成功，需要点击<a href='"+ url +"'>这里</a>激活。");
//        应按实际用户的邮箱填写，但情况不允许
        email.setTo("wjx.nit@qq.com");
//        耗时最长，使用异步解决
        new Thread(()->{
            emailUtils.sendEmail(email);
        }).start();
    }

    @Override
    public User getUserByEmail(String email) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", email);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer updateUserStatus(String code) {
        return userMapper.updateUserStatus(code);
    }

    @Override
    public User getUserByUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return userMapper.selectOne(queryWrapper);
    }


}
